Bitwise XOR Operator (^)
The ^ operator (also known as the XOR operator) stands for Exclusive Or. Here, if bits in the compared position do not match their resulting bit is 1. i.e, The result of the bitwise XOR operator is 1 if the corresponding bits of two operands are opposite, otherwise 0.
Example:
Take two bit values X and Y, where X = 7= (111)2 and Y = 4 = (100)2 . Take Bitwise and of both X & y
Explanation: On the basis of truth table of bitwise XOR operator we can conclude that the result of
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0We used the similar concept of bitwise operator that are show in the image.
Implementation of XOR operator:
C++
#include <iostream> using namespace std; int main() { int a = 12, b = 25; cout << (a ^ b); return 0; } |
Java
import java.io.*; class GFG { public static void main(String[] args) { int a = 12 , b = 25 ; int result = a ^ b; System.out.println(result); } } // This code is contributed by garg28harsh. |
Python3
a = 12 b = 25 result = a ^ b print (result) # This code is contributed by garg28harsh. |
C#
// C# Code using System; public class GFG { static public void Main() { // Code int a = 12, b = 25; int result = a ^ b; Console.WriteLine(result); } } // This code is contributed by lokesh |
Javascript
let a = 12; let b = 25; console.log((a ^ b)); // This code is contributed by akashish__ |
21
Time Complexity: O(1)
Auxiliary Space: O(1)
Complete Reference for Bitwise Operators in Programming/Coding
There exists no programming language that doesn’t use Bit Manipulations. Bit manipulation is all about these bitwise operations. They improve the efficiency of programs by being primitive, fast actions. There are different bitwise operations used in bit manipulation. These Bitwise Operators operate on the individual bits of the bit patterns. Bit operations are fast and can be used in optimizing time complexity.
Table of Content
- 1. Bitwise AND Operator (&)
- 2. Bitwise OR Operator (|)
- 3. Bitwise XOR Operator (^)
- 4. Bitwise NOT Operator (!~)
- 5. Left-Shift (<<)
- 6. Right-Shift (>>)
Some common bit operators are: